package org.docshare.zhuishu;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import org.docshare.helper.IpHelper;
import org.docshare.http.DataServer;
import org.docshare.http.YangResponse;
import org.docshare.mm.YangDrawerLayout;

public class MainAcitivity extends FragmentActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = "MainAcitivity";

    private boolean isDirection_left;
    private FragmentManager fm;


    //主布局的根，一个DrawerLayout
    @ViewInject(id=R.id.drawer_layout)
    DrawerLayout mDrawerLayout;

    //主布局中左侧边的根
    @ViewInject(id=R.id.left_root)
    LinearLayout leftRoot;

    private YangDrawerLayout yangDrawLayout;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	getMenuInflater().inflate(R.menu.bar, menu);
    	return super.onCreateOptionsMenu(menu);
    }

    PersonFragment person=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FinalActivity.initInjectedView(this);



        //修改ActionBar
        yangDrawLayout = new YangDrawerLayout(this,mDrawerLayout,leftRoot);

        
        fm = getSupportFragmentManager();
        initLeft(); //设置抽屉的Fragment
        //initFav();  //设置主页为FavFragment

        GlobalConfig.main_menu_listener = this;
        GlobalConfig.mainAcitivity = this;

        //登陆-测试用
        DataServer.getIns(this).login("admin", "Ytf19851206", new YangResponse() {

            public void onResult(boolean succ, Object response) {
                Log.i(TAG, "onResult: 登陆结果:"+succ+", resp = "+response);
            }
        });
        
        person = new PersonFragment();
        Log.i("MainActivity", IpHelper.getIP(this));
        Log.i("MainActivity", IpHelper.getLocalIpAddress());
    }
    
    private void initLeft(){
    	LeftMenuFragment menuF = new LeftMenuFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.left_root,menuF); //左侧边栏
        ft.commit();
    }

    FavFragment favF = new FavFragment();
    
    
    private void initFav(){
    	 //添加主页为
    
        fm = getSupportFragmentManager();

        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.contentf,favF);
        ft.commit();
    }
    
    public void switchToFragment(Fragment f,boolean addToStack){
    	switchToFragment(f, addToStack,null);
    }

    public void switchToFragment(Fragment f,boolean addToStack,String name){
        FragmentTransaction ft = fm.beginTransaction();
        
        ft.replace(R.id.contentf,f);
        if(addToStack)ft.addToBackStack(name);
        ft.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        yangDrawLayout.onOptionsItemSelected(item);
        return super.onOptionsItemSelected(item);
    }

    /**
     * 左侧抽屉的主菜单被点击后会调用此处
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemClick-----------: id="+id);
        Fragment f=null;
        if(id == 0){
        	f = new ConfigFragment("view://fav",null);
        }else if(id == 1){
        	f= new ConfigFragment("view://discover",null);
        }else if(id == 2){
        	f = person;
        }
    	switchToFragment(f, true,f.getClass().getName());
    	mDrawerLayout.closeDrawers();
        

    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(keyCode == KeyEvent.KEYCODE_BACK){
    		//dialogExit(this);
    		//return false;
    	}
    	return super.onKeyDown(keyCode, event);
    }
    public static void dialogExit(Context context) {
        AlertDialog.Builder builder = new Builder(context);
        builder.setMessage("确定要退出吗?");
        builder.setTitle("提示");
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setPositiveButton("确认",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        android.os.Process.killProcess(android.os.Process
                                .myPid());
                    }
                });
       
        builder.setNegativeButton("取消",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
       
        builder.create().show();
}
}
